/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.escom.healthlc.ws;

import com.escom.healthlc.business.ConsultaBusiness;
import com.escom.healthlc.entidad.ConsultaVO;
import com.escom.healthlc.entidad.EstudioORM;
import com.escom.healthlc.utils.Constantes;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.logging.Level;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Carlos
 */
@Controller
@RequestMapping("/ormEstudioWebService")
public class ORMEstudioWebService {

    private static final Logger logger = LoggerFactory.getLogger(ORMEstudioWebService.class);

    @RequestMapping(method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    public @ResponseBody
    EstudioORM getORMEstudioJSON(@RequestParam(value = "estudioORM") String estudioORMString) {
        logger.debug("estudioORM: {}", estudioORMString);
        ObjectMapper mapper = new ObjectMapper();
        EstudioORM estudioORM = null;
        try {
            estudioORM = mapper.readValue(estudioORMString, EstudioORM.class);
        } catch (IOException ex) {
            java.util.logging.Logger.getLogger(ORMEstudioWebService.class.getName()).log(Level.SEVERE, null, ex);
        }

        if (estudioORM != null) {
            String idMedico = estudioORM.getIdMedico();
            String idPaciente = estudioORM.getIdPaciente();
            String path = Constantes.PATH_WEB_VIRTUAL;
            logger.debug(" path {}", path);
            String fileName = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss").format(Calendar.getInstance().getTime())
                    + "-" + idMedico;
            File fileObject = new File(path + fileName + ".txt");
            ConsultaVO consulta = new ConsultaVO();
            consulta.setIdMedico(idMedico);
            consulta.setIdPaciente(idPaciente);
            consulta.setArchivoPrueba(fileName);
            ApplicationContext appContext;
            try {
                fileObject.createNewFile();
                appContext = new FileSystemXmlApplicationContext(Constantes.PATH_APP_CONTEXT);
                ConsultaBusiness consultaBusiness = appContext.getBean(ConsultaBusiness.class);
                consultaBusiness.actualizarArchivoPrueba(consulta);
                ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(fileObject));
                os.writeObject(estudioORM);
                os.close();
            } catch (IOException ex) {
                logger.error("Logger Error: {}", ex.fillInStackTrace().toString());
                java.util.logging.Logger.getLogger(ORMEstudioWebService.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return new EstudioORM();
    }

}
